package com.dongdongshop.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.util.List;
@Controller
@RequestMapping("cartController")
public class CartController {
    @Reference
    private CartService cartService;

    @RequestMapping("showCart")
    public String showCart (){
        return "cart";
    }

    @RequestMapping("getCartList")
    @ResponseBody
    public Result getCartList(HttpServletRequest request, HttpServletResponse response){
        List<Cart> cartList = getCookieValue(request, response);
        return Result.build(ConstantEnum.SUCCESS).setResult(cartList);//返回的是我们定义的Car对象
    }


    //从cookie中获取购物车数据
    @RequestMapping("getCart")
    @ResponseBody
    public List<Cart> getCookieValue(HttpServletRequest request,HttpServletResponse response){
        //判断用登陆态
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        String cookieList = CookieUtils.getCookieValue(request, "cartList",true);
        if(StringUtils.isBlank(cookieList)){//这里如果cookieList为空返回的为true
            cookieList="[]";//ruguo为空我们定义一个空的中括号字符串
        }
        List<Cart> carts = JSONObject.parseArray(cookieList, Cart.class);//将中括号转为Car对象
        if(user == null){//如果用户未登录
            return carts;
        }else{//如果用户已经登录将数据存在redis中,这时cookie中的数据和redis中的数据是不关联的
            List<Cart> listCartFromRedis = cartService.findListCartFromRedis(user.getUsername());
            //判断cookie中是否有数据有就合并
            if(carts.size()>0){
                //合并购物车
                List<Cart> cartList = cartService.merageCartList(carts, listCartFromRedis);
                //将合并后的数据放到redis中
                cartService.addCartListToRedis(cartList,user.getUsername());
                //清空cookie数据
                CookieUtils.deleteCookie(request,response,"cartList");
                return  cartList;
            }
                return listCartFromRedis;
        }
    }
    @RequestMapping("addCart")
    @ResponseBody
    public List<Cart> toAddCart(HttpServletRequest request, HttpServletResponse response,Long itemId,Integer num){
        //先从cookie中获取购物车
        List<Cart> cartList = getCookieValue(request,response);
        //调用购物车接口,添加购物车数据
        cartList = cartService.addCart(cartList,itemId,num);
        //获取登录用户
        TbUser tbUser  = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if(tbUser==null){
            String cartListJSON = JSONObject.toJSONString(cartList);//如果未登录存cookie
            CookieUtils.setCookie(request,response,"cartList",cartListJSON,true);
        }else{
        //如果已登录存入redis
            cartService.addCartListToRedis(cartList,tbUser.getUsername());
        }
        return cartList;
    }

    @RequestMapping("addCookieTest")
    @ResponseBody
    public String toAdd(HttpServletRequest request, HttpServletResponse response){
        CookieUtils.setCookie(request,response,"mycookie","ceshi",true);
        return "OK";
    }
    //cookie只以字符串类型存储cookie
    @RequestMapping("getCookieTest")
    @ResponseBody
    public String getCookie(HttpServletRequest request){
        String mycookie = CookieUtils.getCookieValue(request, "mycookie", true);
        return mycookie;
    }
    //删除
    @RequestMapping("deleteCookieTest")
    @ResponseBody
    public String toDelete(HttpServletRequest request,HttpServletResponse response) {
        CookieUtils.deleteCookie(request ,response ,"mycookie");
        return "OK";
    }
    //判断用户有没有登录
    @RequestMapping("existUser")
    @ResponseBody
    public Result existUser(){
        TbUser tbUser  = (TbUser) SecurityUtils.getSubject().getPrincipal();
        return Result.build(ConstantEnum.SUCCESS).setResult(tbUser);
    }
    //跳转订单页面
    @RequestMapping("getOrderInfo")
    public String getOrderInfo(){
        return "getOrderInfo";
    }
}
